<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserChannels;
use plugin\ycMassage\app\model\MassageUserChannelsCategory;
use plugin\ycMassage\utils\enum\State;
use support\Request;
use think\facade\Db;

class ChannelsCategoryController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('name', 'input', '类目', [
            'placeholder' => '类目搜索'
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addTopButton('add', '添加类目', [
            'api'       => $this->pluginPrefix . '/admin/ChannelsCategory/create',
            'path' => '/ChannelsCategory/create',
            'type'      => 'modal'
        ], [
            'title' => '添加类目',
        ], [
            'type' => 'success'
        ]);
        $builder->addRightButton('update', '修改', [
            'api'    => $this->pluginPrefix . '/admin/ChannelsCategory/update',
            'path' => '/ChannelsCategory/update',
            'type'      => 'modal'
        ], [], [
            'type' => 'primary',
            'link' => true
        ]);
        $builder->addRightButton('del', '删除', [
            'type'   => 'confirm',
            'api'    => $this->pluginPrefix . '/admin/ChannelsCategory/delete',
            'method' => 'delete'
        ], [
            'title'   => '温馨提示',
            'content' => '是否确认删除该数据',
        ], [
            'type' => 'error',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumn('title', '类目', [])
            ->addColumnEle('thumb', '图片', [
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumnEle('state', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'switch',
                    'api' => $this->pluginPrefix . '/admin/ChannelsCategory/updateState',
                    'checked' => State::YES,
                    'unchecked' => State::NO
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['img.saas_appid', '=', $request->saas_appid]
        ];
        $mch_id = $request->get('mch_id');
        if ($mch_id) {
            $where[] = ['img.mch_id', '=', $mch_id];
        }
        $name = $request->get('name');
        if ($name) {
            $where[] = ['img.title', 'like', "%{$name}%"];
        }
        $examine = $request->get('examine');
        if ($examine !== null) {
            $where[] = ['img.examine', '=', $examine];
        }
        $data = MassageUserChannelsCategory::where($where)
            ->alias('img')
            ->order('img.create_time desc')
            ->field('img.*')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
            });
        return $this->successRes($data);
    }
    public function create(Request $request)
    {
        $saas_appid = $request->saas_appid;
        if ($request->method() === 'POST') {
            $D = $request->post();
            Db::startTrans();
            try {
                $MassageUserChannelsCategory = new MassageUserChannelsCategory;
                $D['saas_appid'] = $saas_appid;
                if (!empty($D['thumb'])) {
                    $D['thumb'] = $D['thumb'][0];
                }
                $MassageUserChannelsCategory->save($D);
                Db::commit();
                return $this->success('添加成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('title', 'input', '类目名称', '', [
            'col'           => 12
        ]);
        $builder->addComponent('thumb', 'uploadify', '类目图片', '', [
            'col' => 12,
            'props' => [
                'type' => 'image',
                'limit' => 1,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $builder->addRow('state', 'radio', '状态', State::YES['value'], [
            'col' => 24,
            'options' => State::getOptions()
        ]);
        $data = $builder->create();
        return $this->successRes($data);
    }
    public function update(Request $request)
    {
        $id = $request->get('id');
        $MassageUserChannelsCategory = MassageUserChannelsCategory::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageUserChannelsCategory) {
            return $this->fail('数据不存在');
        }
        if ($request->method() === 'POST') {
            $D = $request->post();
            Db::startTrans();
            try {
                if (empty($D['thumb'])) {
                    $D['thumb'] = null;
                }else{
                    $D['thumb'] = $D['thumb'][0];
                }
                $MassageUserChannelsCategory->save($D);
                Db::commit();
                return $this->success('修改成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }
        $MassageUserChannelsCategory->thumb = [];
        if(!empty($MassageUserChannelsCategory['thumb'])){
            $MassageUserChannelsCategory->thumb[] = $MassageUserChannelsCategory['thumb'];
        }
        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('title', 'input', '类目名称', '', [
            'col'           => 12
        ]);
        $builder->addComponent('thumb', 'uploadify', '类目图片', '', [
            'col' => 12,
            'props' => [
                'type' => 'image',
                'limit' => 1,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $builder->addRow('state', 'radio', '状态', State::YES['value'], [
            'col' => 24,
            'options' => State::getOptions()
        ]);
        $data = $builder->setData($MassageUserChannelsCategory)->create();
        return $this->successRes($data);
    }
    public function updateState(Request $request)
    {
        $id = $request->post('id');
        $MassageUserChannelsCategory = MassageUserChannelsCategory::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        $MassageUserChannelsCategory->state = $MassageUserChannelsCategory->state == State::YES['value'] ? State::NO['value'] : State::YES['value'];
        if ($MassageUserChannelsCategory->save()) {
            return $this->success('修改成功');
        }
        return $this->fail('修改失败');
    }
    public function delete(Request $request)
    {
        $id = $request->post('id');
        $where = [
            ['saas_appid', '=', $request->saas_appid],
            ['id', '=', $id]
        ];
        $MassageUserChannelsCategory = MassageUserChannelsCategory::where($where)->find();
        if (!$MassageUserChannelsCategory) {
            return $this->fail('数据不存在');
        }
        if(MassageUserChannels::where(['category_id' => $id])->count()){
            return $this->fail('该类目下有数据，不能删除');
        }
        if ($MassageUserChannelsCategory->delete()) {
            return $this->success('删除成功');
        }
        return $this->fail('删除失败');
    }
}
